package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-21 18:22
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;


    //保存交易记录
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, PaymentType paymentType) {

        // 在交易记录表中  orderId对应的支付方式只能有一条记录
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id", orderInfo.getId());
        paymentInfoQueryWrapper.eq("payment_type", paymentType.name());
        PaymentInfo paymentInfo1 = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);

        //判断当前表中是否有这条记录
        if (paymentInfo1!=null){
            return;
        }
        //保存的业务逻辑
        PaymentInfo paymentInfo = new PaymentInfo();
        //赋值paymentInfo
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType.name());
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setCreateTime(new Date());

        paymentInfoMapper.insert(paymentInfo);
    }


    //根据第三方交易标号 支付类型查询交易记录
    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String name) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type", name);
        return paymentInfoMapper.selectOne(paymentInfoQueryWrapper);

    }


    //根据第三方交易标号 支付类型更新交易状态
    @Override
    public void paySuccess(String outTradeNo, String name, Map<String, String> paramMap) {
        //如果这条记录是关闭状态  或者已经是PAID了  则还需要走下面的更新代码吗？  不需要
        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, name);
        if (("PAID").equals(paymentInfoQuery.getPaymentStatus())||("CLOSED").equals(paymentInfoQuery.getPaymentStatus())){
            //如果符合条件  则不需要继续执行了
            return ;
        }

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setTradeNo(paramMap.get("trade_no"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackContent(paramMap.toString());

//        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
//        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
//        paymentInfoQueryWrapper.eq("payment_type", name);

        //paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper );

        //发送个什么东西？orderId
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());


        this.updatePaymentInfo(outTradeNo,name,paymentInfo);

    }

    //方便方法复用
    public void updatePaymentInfo(String outTradeNo, String name, PaymentInfo paymentInfo) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type", name);

        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);

    }


    //取消订单
    @Override
    public void closePayment(Long orderId) {
        //设置关闭交易记录的条件
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();

        paymentInfoQueryWrapper.eq("order_id", orderId);
        //如果当前的交易记录不存在  咋不更新交易记录
        Integer count = paymentInfoMapper.selectCount(paymentInfoQueryWrapper);
        if (count==null||count.intValue()==0){
            return;
        }
        //在关闭支付宝交易之前  还需要关闭paymentInfo
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
        paymentInfoMapper.update(paymentInfo, paymentInfoQueryWrapper);
    }
}
